package com.blog.controller;

import com.blog.cache.CacheConstants;
import com.blog.entity.Category;
import com.blog.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private CategoryMapper categoryMapper;

    @Cacheable(value = CacheConstants.CAT,sync = true)
    @GetMapping("/category/list")
    public List<Category> list(){
        System.out.println("===list()==");
        return categoryMapper.list();
    }

    @PostMapping("/category/insert")
    public void insert(@RequestBody  Category category){
        categoryMapper.insert(category);
    }

    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CAT_LIST, allEntries = true)
    })
    @DeleteMapping("/category/delete/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        categoryMapper.deleteById(id);
    }
}
